package com.zhaogang.controller;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zhaogang.dto.GoodsInfoResponse;
import com.zhaogang.dto.OrderInfoResponse;
import com.zhaogang.dto.ReceiptInfoRequest;
import com.zhaogang.entity.Order;
import com.zhaogang.entity.SaleGoods;
import com.zhaogang.entity.UserAccount;
import com.zhaogang.entity.UserDetails;
import com.zhaogang.service.GoodsService;
import com.zhaogang.service.OrderService;
import com.zhaogang.service.UserDetailsService;

@Controller
@RequestMapping("/order")
public class OrderController {
	
	@Autowired
	private OrderService orderService;
	
	@Autowired
	private UserDetailsService userDetailsService;
	
	@Autowired
	private GoodsService goodsService;
	
	//显示商品信息
	@ResponseBody
	@RequestMapping("/showOrder")
	public ModelAndView showOrder(HttpServletRequest request) throws UnsupportedEncodingException{
		ModelAndView mv=null;
		
		//从session中得到用户账户对象
		UserAccount userAccount = (UserAccount) request.getSession().getAttribute("userToken");//usertOKEN
		System.out.println(userAccount);
		//判断是否为空 为空表明没有登录，跳到登录界面
		//否则继续往下进行
		if(userAccount == null){
			//String redirUrl = request.get
			System.out.println("redirUrl:"+request.getHeader("referer"));
			mv = new ModelAndView("redirect:/pages/user/login.jsp?redirUrl="+request.getHeader("referer"));
			return mv;
		}
		System.out.println(userAccount);
		
		//获取详情页面的购买参数
		request.setCharacterEncoding("utf-8");
		int userSum = Integer.parseInt(request.getParameter("userSum"));//购买数量
		Integer saleGoodsId = Integer.parseInt(request.getParameter("id"));//商品商家id
		double single_price = Double.valueOf(request.getParameter("single_price"));
		//组装order对象
		//得到某一账号的用户详情对象		
		UserDetails userDetails = userDetailsService.queryUserDetailByuserAccount(userAccount.getId());
		//获取商家商品详情
		SaleGoods saleGoods = goodsService.queryBysale_goodsId(saleGoodsId);
		
		 
		
		 GoodsInfoResponse oir =  orderService.selectSaleGoodsInfo(saleGoodsId);
		
		
		
		if(userDetails != null){			
			oir.setRecAddress(userDetails.getAddress());
			oir.setRecPhone(userDetails.getPhone());
			oir.setRecName(userDetails.getRealname());
		}
		
		
		
		oir.setSinglePrice(single_price);
		oir.setNum(userSum);
		oir.setSumPrice(userSum*single_price);
		oir.setUserAccountId(userAccount.getId());		
		
		
		
		mv = new ModelAndView("order/showOrder","orderInfo",oir);
		return mv;
	}
	
	
	//提交订单付款,修改订单状态为已完成
	@ResponseBody
	@RequestMapping("/payOrder")
	public ModelAndView payOrder(HttpServletRequest req){
		ModelAndView mv = new ModelAndView();
		if(req.getParameter("orderId") == ""){
			mv.setViewName("user/productList");
			mv.addObject("info", "<script>alert('订单已取消,请重新购买!')</script>");
			return mv;
		}
		int orderId = Integer.parseInt(req.getParameter("orderId"));
		int row = orderService.updateValid(orderId);
		if(row>0){
			mv.setViewName("user/payment");
			return mv;
		}else{
			mv.setViewName("user/productList");
			mv.addObject("info", "<script>alert('订单已取消,请重新购买!')</script>");
			return mv;
		}
	}
	
	//提交订单，减少库存，锁定商品为不可售，进入支付页面
	@RequestMapping("/subOrder")
	public ModelAndView subOrder(HttpServletRequest req) throws UnsupportedEncodingException{
		req.setCharacterEncoding("utf-8");
		
		ModelAndView mv = null;
		int saleGoodsId = Integer.parseInt(req.getParameter("saleGoodsId"));
		int userAccountId = Integer.parseInt(req.getParameter("userAccountId"));
		double singlePrice = Double.parseDouble(req.getParameter("singlePrice"));
		int num = Integer.parseInt(req.getParameter("num"));
		double sumPrice = Double.parseDouble(req.getParameter("sumPrice"));
		String recAddress = req.getParameter("recAddress");
		String recPhone = req.getParameter("recPhone");
		String recName = req.getParameter("recName");
		Order o = new Order();
		o.setNum(num);
		o.setOrderTime(new Date());
		o.setRecAddress(recAddress);
		o.setRecName(recName);
		o.setRecPhone(recPhone);
		o.setSaleGoodsId(saleGoodsId);
		o.setSinglePrice(singlePrice);
		o.setState(0);
		o.setSumPrice(sumPrice);
		o.setUserAccountId(userAccountId);
		
		//订单生成与否，界面的跳转
		if(orderService.AddOrder(o)){
			//System.out.println("success");
			
			OrderInfoResponse oir = orderService.selectOrder(o.getId());
			mv = new ModelAndView("order/payOrder","orderInfo",oir);
			return mv;
		}else{
			//System.out.println("error");
			mv = new ModelAndView("redirect:../goods/detail?id="+saleGoodsId);
			return mv;
		}
		
		
		
		
		//int row = orderService.updateValid(orderId);
//		if(row>0){
//			mv.setViewName("order/success");
//			return mv;
//		}
		
	}
	
	//添加收货人信息
	@ResponseBody
	@RequestMapping("/addReceiptInfo")
	public int addReceiptInfo(HttpServletRequest req){
		
		String recAddress = req.getParameter("recAddress");
		String recPerson = req.getParameter("recPerson");
		String recPhone = req.getParameter("recPhone");
		Integer orderId = null;
		System.out.println("orId:"+req.getParameter("orderId"));
		int row = 0;
		if(!"".equals(req.getParameter("orderId"))){
			orderId = Integer.parseInt(req.getParameter("orderId"));
			ReceiptInfoRequest rip = new ReceiptInfoRequest(orderId,recAddress,recPerson,recPhone);
			 row = orderService.updateReceiptInfo(rip);
		}
		return row;
	}
	
	//删除订单，增加库存，修改商品为可售
	@ResponseBody
	@RequestMapping("/delOrder")
	public ModelAndView delOrder(HttpServletRequest req){
		ModelAndView mv = new ModelAndView();
		if(!"".equals(req.getParameter("orderId"))){
			int orderId = Integer.parseInt(req.getParameter("orderId"));
			
			boolean row = orderService.delOrder(orderId);
			if(row){
				mv.setViewName("user/productList");
				return mv;
			}else{
				mv.setViewName("user/productList");
				mv.addObject("info", "<script>alert('订单已取消,请重新购买!')</script>");
				return mv;
			}
		}else{
			mv.setViewName("user/productList");
			mv.addObject("info", "<script>alert('订单已取消,请重新购买!')</script>");
			return mv;
		}
		
	}
	
	
}
